#include "InSdkIniBasic.h"

#define DEF_TAG "IniBasic"

typedef enum
{
    INI_BASIC_KEY_VIDEO = 0,
    INI_BASIC_KEY_AUDIO,
    INI_BASIC_KEY_LED,
    INI_BASIC_KEY_TIME,
    INI_BASIC_KEY_MAX
} EnInSdkIniBasicKeyNum;

pthread_mutex_t g_InSdkIniBasic_Mutex;

StInSdkIniCfgKeyInfo g_InSdkIniBasic_Key[INI_BASIC_KEY_MAX] = {
    {INI_BASIC_KEY_VIDEO, "video"},
    {INI_BASIC_KEY_AUDIO, "audio"},
    {INI_BASIC_KEY_LED, "led"},
    {INI_BASIC_KEY_TIME, "time"},
};

StInSdkIniCfgKeyInfo g_InSdkIniBasic_Info[INI_BASIC_MAX] = {
    {INI_BASIC_VIDEO_FLIP, "video:flip"},
    {INI_BASIC_VIDEO_IRCUT_REVERSE, "video:ircut_reverse"},
    {INI_BASIC_VIDEO_NIGHT_MODE_SW, "video:night_mode_sw"},
    {INI_BASIC_AUDIO_SPK_VOL, "audio:spk_vol"},
    {INI_BASIC_AUDIO_SPK_GAIN, "audio:spk_gain"},
    {INI_BASIC_AUDIO_MIC_VOL, "audio:mic_vol"},
    {INI_BASIC_AUDIO_MIC_GAIN, "audio:mic_gain"},
    {INI_BASIC_LED_LINK, "led:link"},
    {INI_BASIC_LED_WATCH, "led:watch"},
    {INI_BASIC_TIME_TZ_SEC, "time:tz_sec"},
};

Com_s32 g_InSdkIniBasic_Val[INI_BASIC_MAX];

Com_void Ext_InSdkIniBasic_SetVal(EnInSdkIniBasicIndex key, Com_s32 val)
{
    pthread_mutex_lock(&g_InSdkIniBasic_Mutex);

    Com_s8 buff[32];
    memset(buff, 0, sizeof(buff));

    struct config *conf = conf_load(DEF_INI_BASIC_CONFIG);
    snprintf(buff, sizeof(buff), "%d", val);
    conf_set_string(conf, g_InSdkIniBasic_Info[key].string, buff);
    conf_save(conf);
    conf_unload(conf);

    g_InSdkIniBasic_Val[key] = val;
    pthread_mutex_unlock(&g_InSdkIniBasic_Mutex);
}

Com_void Ext_InSdkIniBasic_GetAll(Com_void)
{
    pthread_mutex_lock(&g_InSdkIniBasic_Mutex);

    Com_s32 i = 0;
    struct config *conf = conf_load(DEF_INI_BASIC_CONFIG);
    pStInSdkIniCfgKeyInfo basic_info = g_InSdkIniBasic_Info;

    for (i = 0; i < INI_BASIC_MAX; i++)
    {
        g_InSdkIniBasic_Val[i] = 0;
        g_InSdkIniBasic_Val[i] = conf_get_int(conf, basic_info[i].string);
    }

    conf_unload(conf);
    pthread_mutex_unlock(&g_InSdkIniBasic_Mutex);
}

Com_s32 Ext_InSdkIniBasic_GetVal(EnInSdkIniBasicIndex dp)
{
    return g_InSdkIniBasic_Val[dp];
}

Com_void InSdkIniBasic_SetDefault(Com_void)
{
    struct config *conf = conf_load(DEF_INI_BASIC_CONFIG);
    pStInSdkIniCfgKeyInfo basic_info = g_InSdkIniBasic_Info;

    if ((conf_get_string(conf, basic_info[INI_BASIC_VIDEO_FLIP].string) == NULL) ||
        (conf_get_string(conf, basic_info[INI_BASIC_AUDIO_SPK_VOL]) == NULL) ||
        (conf_get_string(conf, basic_info[INI_BASIC_LED_LINK]) == NULL) ||
        (conf_get_string(conf, basic_info[INI_BASIC_TIME_TZ_SEC].string) == NULL))
    {
        int i;
        for (i = 0; i < INI_BASIC_KEY_MAX; i++)
        {
            Ext_InSdkIniCfg_Recover(conf, g_InSdkIniBasic_Key[i].string);
        }

#if CMAKE_DEFAULT_FLIP
        conf_set_string(conf, basic_info[INI_BASIC_VIDEO_FLIP].string, "1");
#else
        conf_set_string(conf, basic_info[INI_BASIC_VIDEO_FLIP].string, "0");
#endif

#if CMAKE_IRCUT_REVERSE
        conf_set_string(conf, basic_info[INI_BASIC_VIDEO_IRCUT_REVERSE].string, "1");
#else
        conf_set_string(conf, basic_info[INI_BASIC_VIDEO_IRCUT_REVERSE].string, "0");
#endif

        conf_set_string(conf, basic_info[INI_BASIC_VIDEO_NIGHT_MODE_SW].string, "0");
        conf_set_string(conf, basic_info[INI_BASIC_AUDIO_SPK_VOL].string, "6");
        conf_set_string(conf, basic_info[INI_BASIC_AUDIO_SPK_GAIN].string, "3");
        conf_set_string(conf, basic_info[INI_BASIC_AUDIO_MIC_VOL].string, "6");
        conf_set_string(conf, basic_info[INI_BASIC_AUDIO_MIC_GAIN].string, "3");
        conf_set_string(conf, basic_info[INI_BASIC_LED_LINK].string, "1");
        conf_set_string(conf, basic_info[INI_BASIC_LED_WATCH].string, "1");
        conf_set_string(conf, basic_info[INI_BASIC_TIME_TZ_SEC].string, "28800");
        conf_save(conf);
    }

    conf_unload(conf);
}

Com_void Ext_InSdkIniBasic_Init(Com_void)
{
    pthread_mutex_init(&g_InSdkIniBasic_Mutex, NULL);
    Ext_InSdkIniCfg_Check(DEF_INI_BASIC_CONFIG);
    InSdkIniBasic_SetDefault();
    Ext_InSdkIniBasic_GetAll();
}

Com_void Ext_InSdkIniBasic_Exit(Com_void)
{
    pthread_mutex_destroy(&g_InSdkIniBasic_Mutex);
}
